package com.example.demo.application_event_publisher;

import com.example.demo.application_event_publisher.User;
import com.example.demo.application_event_publisher.UserRegisterEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private ApplicationEventPublisher publisher;

    @GetMapping("/register")
    public String register(String name, int age) {
        User user = User.builder().name(name).age(age).build();
        // 事件发布
        publisher.publishEvent(new UserRegisterEvent(user));

        log.info("用户[" + user.getName() + "]注册完成");
        return "success";
    }
}